package Dao;

import DataClass.ResearchProject;
import Util.JBDCUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ResearchProjectDao {

    private static PreparedStatement getResearchProjectStatement = null;

    private static PreparedStatement findResearchProjectStatement = null;

    public static List<ResearchProject> getResearchProject() throws SQLException {
        List<ResearchProject> list = new ArrayList<>();
        ResultSet data;
        try{
            data = ResearchProjectDao.getResearchProjectData();
        }catch (SQLException e){
            System.out.println("科研项目查询失败 from Dao");
            throw new SQLException();
        }

        while(data.next()){
            String RPid  = data.getString("RPno");
            String RPname = data.getString("RPname");
            String RPtype = data.getString("RPtype");
            String RPstudent = data.getString("RPstudent");
            String RPtime = data.getString("RPtime");
            String RPteacher = data.getString("RPteacher");
            list.add(new ResearchProject(RPid,RPname,RPtype,RPstudent,RPtime,RPteacher));
        }
        JBDCUtil.closeResultSet(data);
        return list;
    }

    /*获取科研信息*/
    public static ResultSet getResearchProjectData() throws SQLException {
        if(getResearchProjectStatement == null) {
            String sql = "SELECT * FROM ResearchProject";
            getResearchProjectStatement = JBDCUtil.getPreparedStatement(sql);
        }
        ResultSet data = getResearchProjectStatement.executeQuery();
        JBDCUtil.commit();
        return data;
    }
    /*关键词查询*/
    public static ResultSet findResearchProject(String KeyWord) throws SQLException{

        if(findResearchProjectStatement == null) {
            String sql = "SELECT * FROM ResearchProject WHERE RPname =?";
            findResearchProjectStatement = JBDCUtil.getPreparedStatement(sql);
        }

        findResearchProjectStatement.setString(1,KeyWord);
        System.out.println("查询关键词"+KeyWord);
        ResultSet result = findResearchProjectStatement.executeQuery();
        JBDCUtil.commit();
        return result;
    }
}